package com.koi.web.controller.demo.easyexecl;

import com.alibaba.fastjson2.JSON;
import com.koi.common.ex.koi.ParamsException;
import com.koi.common.utils.file.FileTypeUtil;
import com.koi.easyexcel.bigdata.BigDataEasyExcelUtil;
import com.koi.easyexcel.core.utils.EasyExcelUtil;
import com.koi.plus.mapper.system.SysLoginUserMapper;
import com.koi.pojo.system.SysLoginUser;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.util.*;

/**
 * @author ･ᴗ･
 * @description [ExcelServiceImpl导出业务实现]
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class ExcelServiceImpl implements ExcelService {

    private final EasyExcelUtil easyExcelUtil;

    private final SysLoginUserMapper loginUserMapper;

    /**
     * excel导入-单个sheet
     *
     * @param multipartFile 文件流
     */
    @Override
    public void excelImport(MultipartFile multipartFile) {
        try {
            // 判断是否符合excel文件格式
            FileTypeUtil.decideIsExcel(multipartFile);
            // 导入数据的字段类型和实体类要保持一致
            List<ExcelBo> voList = easyExcelUtil.importExcel(multipartFile, ExcelBo.class, null);
            if (CollectionUtils.isNotEmpty(voList)) {
                System.out.println("本次成功导入：" + voList.size() + "条,数据如下：");
                voList.forEach(vo -> {
                    System.out.println("vo=" + vo.toString());
                });

            }
        } catch (Exception e) {
            throw new ParamsException(e.getMessage());
        }
    }

    /**
     * excel导出-单个sheet
     *
     * @param response 响应体
     */
    @Override
    public void excelExport(HttpServletResponse response) {
        try {
            List<ExcelBo> excelBos = structureDate(20);
            easyExcelUtil.exportExcel(response, excelBos, ExcelBo.class, "excel导出-单sheet", "excel导出-单sheet");
        } catch (Exception e) {
            throw new RuntimeException("导出失败");
        }
    }

    /**
     * excel导入-多个sheet
     *
     * @param multipartFile 文件流
     */
    @Override
    public void excelSheetImport(MultipartFile multipartFile) {
        try {
            List<Integer> indexList = new ArrayList<>();
            indexList.add(0);
            indexList.add(1);
            indexList.add(2);
            indexList.add(3);
            indexList.add(4);
            indexList.add(5);
            List<Object> objList = new ArrayList<>();
            objList.add(new ExcelBo());
            objList.add(new ExcelBo());
            objList.add(new ExcelBo());
            objList.add(new ExcelBo());
            objList.add(new ExcelBo());
            objList.add(new ExcelBo());
            List<List<Object>> resultList = easyExcelUtil.importExcelsByIndex(multipartFile, objList, 6, indexList);
            System.out.println("resultList=" + resultList.size());
            resultList.forEach(vo -> {
                List<ExcelBo> voList = JSON.parseArray(JSON.toJSONString(vo), ExcelBo.class);
                System.out.println("voList=" + voList.size() + "条数据");
            });
        } catch (Exception e) {
            throw new RuntimeException("导入失败");
        }
    }

    /**
     * excel导出-多个sheet
     *
     * @param response 响应体
     */
    @Override
    public void excelSheetExport(HttpServletResponse response) {
        try {
            // 构建多集合集合
            List<List<?>> lists = new ArrayList<>();
            lists.add(structureDate(10));
            lists.add(structureDate(20));
            lists.add(structureDate(30));
            lists.add(structureDate(40));
            lists.add(structureDate(50));
            lists.add(structureDate(60));
            // Sheet页初始化
            Map<Integer, String> clazzMap = new HashMap<>();
            clazzMap.put(0, "一年级成绩表");
            clazzMap.put(1, "二年级成绩表");
            clazzMap.put(2, "三年级成绩表");
            clazzMap.put(3, "四年级成绩表");
            clazzMap.put(4, "五年级成绩表");
            clazzMap.put(5, "六年级成绩表");
            easyExcelUtil.exportExcels(response, Collections.singletonList(lists), clazzMap, "excel导出-多sheet");
        } catch (Exception e) {
            throw new RuntimeException("导出失败");
        }
    }

    /**
     * excel模板导出-单个sheet
     *
     * @param response 响应流
     */
    @Override
    public void excelTemplate(HttpServletResponse response) {
        try {
            List<ExcelBo> list = new ArrayList<>();
            easyExcelUtil.exportExcel(response, list, ExcelBo.class, "excel导出-单sheet模版", "下载模版单sheet");;
        } catch (Exception e) {
            log.error("导出错误：{}", e.getMessage());
            throw new RuntimeException("导出错误");
        }
    }

    /**
     * excel模板导出-多个sheet
     *
     * @param response 响应流
     */
    @Override
    public void excelSheetTemplate(HttpServletResponse response) {
        try {
            // 构建多集合集合
            List<List<?>> lists = new ArrayList<>();
            lists.add(structureDate(1));
            lists.add(structureDate(1));
            lists.add(structureDate(1));
            lists.add(structureDate(1));
            lists.add(structureDate(1));
            lists.add(structureDate(1));
            // Sheet页初始化
            Map<Integer, String> clazzMap = new HashMap<>();
            clazzMap.put(0, "一年级成绩表");
            clazzMap.put(1, "二年级成绩表");
            clazzMap.put(2, "三年级成绩表");
            clazzMap.put(3, "四年级成绩表");
            clazzMap.put(4, "五年级成绩表");
            clazzMap.put(5, "六年级成绩表");
            easyExcelUtil.exportExcels(response, Collections.singletonList(lists), clazzMap, "excel导出-多sheet");
        } catch (Exception e) {
            throw new RuntimeException("导出失败", e);
        }
    }

    /**
     * 构造数据
     *
     * @return 数据集合
     */
    private List<ExcelBo> structureDate(Integer size) {
        List<ExcelBo> list = new ArrayList<>();
        for (int i = 0; i < size; i++) {
            ExcelBo vo = new ExcelBo();
            vo.setId(i);
            vo.setAge(i);
            vo.setName("张三" + i);
            vo.setWen(new Random().nextInt(99));
            vo.setRichard(new Random().nextInt(99));
            vo.setSum(vo.getWen() + vo.getRichard());
            list.add(vo);
        }
        return list;
    }

    /**
     * @param response
     * @description [大数据导出测试]
     * @author ･ᴗ･
     * @method bigDataEasyExecl
     * @date 2023/3/6 9:38
     */
    @Override
    public void bigDataEasyExecl(HttpServletResponse response) throws Exception {
        try {
            // 查询要导出的明细信息
            List<SysLoginUser> modelList = loginUserMapper.selectList(null);
            BigDataEasyExcelUtil.writeExcel(response, modelList, "大数据导出", SysLoginUser.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
